<?php

namespace App\Http\Controllers\Tool;
use App\Http\Controllers\Controller;
use App\Libs\ResponseMessage;
use App\Models\AmapCityCode;
use Illuminate\Http\Request;

class AreaController extends Controller {

    public function getArea(Request $request){
        $parentId = $request->get('parent_id', 1);
        $level = $request->get('level');
        $city = [];
        $district = [];

        if($level === 'city'){
            $city = AmapCityCode::where('parent_id', $parentId)->orderBy('adcode', 'asc')->orderBy('city_code', 'asc')->get();
            if(!empty($city)){
                $district = AmapCityCode::where('parent_id', $city[0]->id)->orderBy('adcode', 'asc')->orderBy('city_code', 'asc')->get();
            }
        }elseif($level === 'district'){
            $district = AmapCityCode::where('parent_id', $parentId)->orderBy('adcode', 'asc')->orderBy('city_code', 'asc')->get();
        }

        return response()->json(ResponseMessage::getInstance()->success([
            'city' => $city,
            'district' => $district
        ])->response());
    }

    public function getAreaInit(Request $request){
        $province = AmapCityCode::where('parent_id', 1)->orderBy('adcode', 'asc')->orderBy('city_code', 'asc')->get();
        $city = AmapCityCode::where('parent_id', 3622)->orderBy('adcode', 'asc')->orderBy('city_code', 'asc')->get();
        $district = AmapCityCode::where('parent_id', 3623)->orderBy('adcode', 'asc')->orderBy('city_code', 'asc')->get();

        return response()->json(ResponseMessage::getInstance()->success([
            'province' => $province,
            'city' => $city,
            'district' => $district
        ])->response());
    }
}